<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>输入框外边距对称性分析</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            color: #333;
            line-height: 1.6;
            padding: 20px;
            min-height: 100vh;
        }

        .container {
            max-width: 1000px;
            margin: 0 auto;
            padding: 20px;
        }

        header {
            text-align: center;
            margin-bottom: 40px;
            padding: 30px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        h1 {
            color: #2c3e50;
            margin-bottom: 15px;
        }

        .description {
            color: #7f8c8d;
            max-width: 800px;
            margin: 0 auto;
        }

        .demo-section {
            display: flex;
            flex-wrap: wrap;
            gap: 30px;
            margin-bottom: 40px;
        }

        .demo-box {
            flex: 1;
            min-width: 300px;
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .demo-title {
            color: #2c3e50;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid #eee;
        }

        textarea {
            width: 100%;
            height: 100px;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 8px;
            resize: vertical;
            font-size: 16px;
            font-family: inherit;
        }

        .code-block {
            background: #2d3436;
            color: #f5f6fa;
            padding: 20px;
            border-radius: 8px;
            overflow-x: auto;
            margin: 20px 0;
            font-family: 'Consolas', monospace;
            line-height: 1.5;
        }

        .comment {
            color: #dfe6e9;
            opacity: 0.8;
        }

        .explanation {
            margin-top: 20px;
            padding: 20px;
            background: #f8f9fa;
            border-left: 4px solid #3498db;
            border-radius: 4px;
        }

        .key-point {
            color: #2c3e50;
            font-weight: bold;
            margin: 10px 0;
        }

        .visual-guide {
            display: flex;
            justify-content: space-between;
            margin: 20px 0;
            padding: 10px;
            background: #f1f2f6;
            border-radius: 8px;
        }

        .margin-box {
            width: 8px;
            height: 60px;
            background: #e74c3c;
            border-radius: 2px;
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            font-weight: bold;
            font-size: 12px;
        }

        .content-box {
            flex-grow: 1;
            height: 60px;
            background: #3498db;
            border-radius: 4px;
            display: flex;
            justify-content: center;
            align-items: center;
            color: white;
            font-weight: bold;
        }

        .footer {
            text-align: center;
            margin-top: 40px;
            color: #7f8c8d;
            font-size: 0.9em;
        }

        @media (max-width: 768px) {
            .demo-section {
                flex-direction: column;
            }
        }
    </style>
</head>

<body>
    <div class="container">
        <header>
            <h1>输入框外边距对称性分析</h1>
            <p class="description">解析为什么在提供的代码中，输入框两侧的外边距是对称的</p>
        </header>

        <div class="demo-section">
            <div class="demo-box">
                <h2 class="demo-title">原始代码效果</h2>
                <p>这是您提供的代码，注意输入框两侧是对称的：</p>

                <div class="code-block">
                    <span class="comment">&lt;!-- 这是您的代码 --&gt;</span><br>
                    * {<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;box-sizing: border-box;<br>
                    }<br>
                    <br>
                    textarea {<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;width: 100%;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;height: 80px;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;font-size: 16px;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;padding: 10px;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;border: 1px solid #ccc;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;border-radius: 8px;<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;resize: vertical;<br>
                    }
                </div>

                <div>
                    <textarea placeholder="请输入要翻译的文本"></textarea>
                </div>

                <div class="explanation">
                    <p class="key-point">为什么输入框两侧的外边距是对称的？</p>
                    <p>在您的代码中，输入框两侧对称的原因如下：</p>
                    <ol>
                        <li><strong>box-sizing: border-box</strong> - 这个设置让元素的宽度计算包含边框和内边距</li>
                        <li><strong>width: 100%</strong> - 输入框宽度设置为父容器的100%</li>
                        <li><strong>没有默认外边距干扰</strong> - 父容器div没有设置内边距或外边距</li>
                        <li><strong>body元素的默认样式</strong> - 现代浏览器通常会给body添加对称的默认外边距</li>
                    </ol>
                </div>
            </div>

            <div class="demo-box">
                <h2 class="demo-title">视觉解析</h2>
                <p>下面是一个可视化演示，展示元素布局：</p>

                <div class="visual-guide">
                    <div class="margin-box">8px</div>
                    <div class="content-box">div容器 (100%宽度)</div>
                    <div class="margin-box">8px</div>
                </div>

                <div class="code-block">
                    <span class="comment">/* body的默认样式（近似值） */</span><br>
                    body {<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;margin: 8px; <span class="comment">/* 大多数浏览器的默认值 */</span><br>
                    }<br>
                    <br>
                    <span class="comment">/* 您的CSS重置了所有元素的box-sizing */</span><br>
                    * {<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;box-sizing: border-box;<br>
                    }<br>
                    <br>
                    <span class="comment">/* textarea继承了box-sizing，宽度计算包含padding和border */</span><br>
                    textarea {<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;width: 100%; <span class="comment">/* 占满父容器宽度 */</span><br>
                    &nbsp;&nbsp;&nbsp;&nbsp;padding: 10px; <span class="comment">/* 内边距 */</span><br>
                    &nbsp;&nbsp;&nbsp;&nbsp;border: 1px solid #ccc; <span class="comment">/* 边框 */</span><br>
                    }
                </div>

                <div class="explanation">
                    <p class="key-point">关键因素：box-sizing: border-box</p>
                    <p>使用<code>box-sizing: border-box</code>后，元素的<code>width</code>属性包含了内容、内边距和边框。</p>
                    <p>这意味着：</p>
                    <ul>
                        <li>textarea的100%宽度 = 父容器的100%宽度</li>
                        <li>textarea的padding和border不会增加总宽度</li>
                        <li>父容器div没有设置padding，所以textarea与div边缘对齐</li>
                        <li>body的默认margin在两侧是对称的</li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="demo-box">
            <h2 class="demo-title">对比实验</h2>
            <p>如果移除<code>box-sizing: border-box</code>，会发生什么？</p>

            <div class="code-block">
                <span class="comment">/* 移除box-sizing: border-box */</span><br>
                textarea {<br>
                &nbsp;&nbsp;&nbsp;&nbsp;width: 100%;<br>
                &nbsp;&nbsp;&nbsp;&nbsp;height: 80px;<br>
                &nbsp;&nbsp;&nbsp;&nbsp;font-size: 16px;<br>
                &nbsp;&nbsp;&nbsp;&nbsp;padding: 10px;<br>
                &nbsp;&nbsp;&nbsp;&nbsp;border: 1px solid #ccc;<br>
                &nbsp;&nbsp;&nbsp;&nbsp;border-radius: 8px;<br>
                &nbsp;&nbsp;&nbsp;&nbsp;resize: vertical;<br>
                &nbsp;&nbsp;&nbsp;&nbsp;<span class="comment">/* box-sizing: content-box (默认值) */</span><br>
                }
            </div>

            <div>
                <textarea placeholder="这个输入框没有使用border-box" style="box-sizing: content-box;"></textarea>
            </div>

            <div class="explanation">
                <p>当使用默认的<code>box-sizing: content-box</code>时：</p>
                <ul>
                    <li>textarea的<code>width: 100%</code>只表示内容区域的宽度</li>
                    <li>padding和border会被添加到总宽度中</li>
                    <li>导致textarea超出父容器宽度（可能出现水平滚动条）</li>
                    <li>但两侧的外边距仍然对称，因为body的默认margin是对称的</li>
                </ul>
            </div>
        </div>

        <div class="footer">
            <p>输入框外边距对称性分析 &copy; 2023</p>
        </div>
    </div>
</body>

</html>